//
//  SuperCoolSegue.m
//  Ratings
//
//  Created by steven.zhan on 14-9-17.
//  Copyright (c) 2014年 hpxbzz. All rights reserved.
//

#import "SuperCoolSegue.h"

@implementation SuperCoolSegue

- (void)perform
{
    UIViewController *source = self.sourceViewController;
    UIViewController *destion = self.destinationViewController;
    
    UIGraphicsBeginImageContext(destion.view.bounds.size);
    [destion.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *destionationImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    UIImageView *destinationImageView = [[UIImageView alloc] initWithImage:destionationImage];
    [source.parentViewController.view addSubview:destinationImageView];
    
    CGAffineTransform scaleTransform=CGAffineTransformMakeScale(0.1, 0.1);
    CGAffineTransform rotateTransform=CGAffineTransformMakeRotation(M_PI);
    destinationImageView.transform=CGAffineTransformConcat(scaleTransform, rotateTransform);
    
    CGPoint oldCenter = destinationImageView.center;
    CGPoint newCenter = CGPointMake(oldCenter.x-destinationImageView.bounds.size.width, oldCenter.y);
    destinationImageView.center=newCenter;
    
    [UIView animateWithDuration:0.5f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^(){
        destinationImageView.transform=CGAffineTransformIdentity;
        destinationImageView.center=oldCenter;
    } completion:^(BOOL done){
        [destinationImageView removeFromSuperview];
        [source presentViewController:destion animated:NO completion:nil];
    }];
    
}

@end
